Shader "SopJoy/Effect/SPro"
{
    Properties
    {
        [Toggle]_Zwrite("Zwrite", float) = 0
        _SrcBlend ("Src", float) = 1.0
        _DstBlend ("Dst", float) = 0.0
        _Cull("_Cull", float) = 2

        _Stencil("_Stencill",range(0,255)) = 0
        _StencilComp("_StencilComp",float) = 2
        _StencilOp("_StencilOp",float) = 6
        _StencilRendMask("_StencilRendMask",range(0,255)) = 255
        _StencilWriteMask("_StencilWriteMask",range(0,255)) = 255



		[Toggle]_RimLight("_RimLight",float) = 1
		[HDR]_RimColor("_RimColor",color) = (1,1,1,1)
		_RimPower("_RimPower",range(0.001,5))=1


        [Toggle(_FLOW_ON)]_Flow("_Flow (RG,A noise)",float)=0
        _FlowTex("_FlowTex",2d)="black"{}
        _FlowSpeed("_FlowSpeed",float)=1
        _FlowStrength("_FlowStrength",range(-0.2,0.2))=0.1
        _FlowOffest("_FlowOffest",range(0,1)) = 0
        _FlowUJumpU("_FlowUJumpU",range(-0.25,0.25))=0.25
        _FlowUJumpV("_FlowUJumpV",range(-0.25,0.25))=0.25
        _FlowTiling("_FlowTiling",float)=1

        _MainTex("_MainTex",2d) = "white"{}

        [Toggle]_VortexAbsorption("VortexAbsorption",float)=0
        _MinMax("_MinMax",vector)=(-1,1,0,0)
        _ObjectCenterOffset("_ObjectCenterOffset",vector)=(0,0,0,0)
		_Stretch("_Stretch",float)=1
		_TargetPos("_TargetPos(objSpace)",vector)=(1,1,1,1)
		_Percent("_Percent",Range(0,1))=0
		_RotateAngle("_RotateAngle(PI)",float)=0

        [Toggle(_USESCENETEX_ON)]_UseSceneTex("_UseScene",float) = 0


        [Toggle(_DISOLVE_ON)]_Disolve("_Disolve",float) = 0
        _DisolveTex("_DisolveTex",2d)="white"{}
        _DisloveAmount("_DisloveAmount",Range(0.0,1.0))=0.0
        _DisloveRimStartColor("_DisloveRimStartColor",color) =(0,0,0,0)
        _DisloveRimEndColor("_DisloveRimEndColor",color) =(0,0,0,0)
        _DisloveRimColorLengt("_DisloveRimColorLengt",float) =0.2
    }
    SubShader
    {
        Tags { "RenderPipeline"="UniversalPipeline" "RenderType" = "Transparent"  "Queue"="Transparent" }
        Pass
        {
            Tags { "LightMode"="UniversalForward" }
            ZWrite [_Zwrite]
            Blend [_SrcBlend] [_DstBlend]
	        Cull [_Cull]
            Stencil
            {
                Ref [_Stencil]
                Comp [_StencilComp]
                Pass [_StencilOp]
                ReadMask[_StencilRendMask]
                WriteMask[_StencilWriteMask]
            }
            HLSLPROGRAM


            #pragma multi_compile _ _FLOW_ON
            #pragma multi_compile _ _USESCENETEX_ON
            #pragma multi_compile _ _DISOLVE_ON
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
                #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
            struct appdata
            {
                UNITY_VERTEX_INPUT_INSTANCE_ID
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                half3 normal:NORMAL;
                #if _DISOLVE_ON
                    float2 uv3:TEXCOORD1;
                #endif
            };
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                half3 normal:TEXCOORD1;
                half3 viewDir:TEXCOORD2;
                #if _DISOLVE_ON
                    float2 disUV:TEXCOORD3;
                #endif
            };
            uniform sampler2D _MainTex,_FlowTex,_DisolveTex;
            CBUFFER_START( UnityPerMaterial )
			uniform half _RimPower,_FlowSpeed,_FlowUJumpU,_FlowUJumpV,_FlowTiling,_FlowStrength,_FlowOffest,_Stretch,_Percent,_RotateAngle,_DisloveAmount,_DisloveRimColorLengt;
            uniform half4 _RimColor;
            uniform half4 _MinMax,_ObjectCenterOffset,_TargetPos,_DisloveDirection;
            uniform half4 _MainTex_ST,_FlowTex_ST,_DisolveTex_ST;
            uniform half4 _DisloveRimStartColor,_DisloveRimEndColor;
			half _RimLight,_VortexAbsorption;
            CBUFFER_END

            #ifdef _USESCENETEX_ON
				#define SampleMainTex(tex,uv) float4( SHADERGRAPH_SAMPLE_SCENE_COLOR(uv), 1.0 );
			#else
				#define SampleMainTex(tex,uv) tex2D(tex,uv);
			#endif
            float3 RotateAroundAxis( float3 center, float3 original, float3 u, float angle )
			{
				original -= center;
				float C = cos( angle );
				float S = sin( angle );
				float t = 1 - C;
				float m00 = t * u.x * u.x + C;
				float m01 = t * u.x * u.y - S * u.z;
				float m02 = t * u.x * u.z + S * u.y;
				float m10 = t * u.x * u.y + S * u.z;
				float m11 = t * u.y * u.y + C;
				float m12 = t * u.y * u.z - S * u.x;
				float m20 = t * u.x * u.z - S * u.y;
				float m21 = t * u.y * u.z + S * u.x;
				float m22 = t * u.z * u.z + C;
				float3x3 finalMatrix = float3x3( m00, m01, m02, m10, m11, m12, m20, m21, m22 );
				return mul( finalMatrix, original ) + center;
			}
            v2f vert (appdata v)
            {
                v2f o;
                
                #if _DISOLVE_ON
                    o.disUV=TRANSFORM_TEX(v.uv3,_DisolveTex);
				#endif

                if(_RimLight>0)
                {
					float3 worldPos=TransformObjectToWorld(v.vertex);
					o.normal=TransformObjectToWorldDir(v.normal);
					float3 worldViewDir = ( _WorldSpaceCameraPos.xyz - worldPos );
                    o.viewDir = normalize(worldViewDir);
                }
                if(_VortexAbsorption)
				{
					half objDis=distance(_ObjectCenterOffset,_TargetPos);
					half vertexDis=distance(_TargetPos,v.vertex.xyz);
					half3 scaleDir=normalize(_TargetPos-_ObjectCenterOffset);
					half nearDis=_MinMax.x + objDis;
					half farDis=_MinMax.y + objDis + 1/_Stretch;
					half clipPercent=lerp(nearDis,farDis,_Percent);
					half percent=saturate((clipPercent-vertexDis)*_Stretch);

					half rotateAng=_RotateAngle*3.14159265*percent;
					half3 rotatePos=RotateAroundAxis(_ObjectCenterOffset,v.vertex.xyz,scaleDir,rotateAng);
					v.vertex.xyz=lerp(rotatePos,_TargetPos,percent);
				}
                o.vertex = TransformObjectToHClip(v.vertex.xyz);    
                #if _USESCENETEX_ON 
					float4 screenPos = ComputeScreenPos(o.vertex);
				#endif
				#ifdef _USESCENETEX_ON
					o.uv.xy= screenPos.xy/screenPos.w;
				#else
					o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
				#endif
                return o;
            }
            float3 GetFlowUV(float2 uv,float2 flowVector,float2 jump,float time,bool flow,float _FlowTiling,float _FlowOffest)
            {
                float phaseOffest =flow?0.5:0;
                float progress =frac(time+phaseOffest);
                float3 uvw;
                uvw.xy  = uv -flowVector*(progress+_FlowOffest);
                uvw.xy+=(time-progress)*jump;
                uvw.xy*=_FlowTiling;
                uvw.z = 1-abs(1-2*progress);
                return uvw;
            }
            half4 frag (v2f i) : SV_Target
            {
                half4 col = SampleMainTex(_MainTex,i.uv);
	            col.a=saturate(col.a+col.a);
                float t = _TimeParameters.x;
                #if _FLOW_ON
                    float3 flowData =tex2D(_FlowTex,i.uv).rga;
                    float2 flowVector = flowData.rg*2-1;
                    flowVector*=_FlowStrength;
                    float noiseTime = t*_FlowSpeed+flowData.z;
                    float2 jump =float2(_FlowUJumpU,_FlowUJumpV);
                    float3 uvwA = GetFlowUV(i.uv,flowVector,jump,noiseTime,false,_FlowTiling,_FlowOffest);
                    float3 uvwB = GetFlowUV(i.uv,flowVector,jump,noiseTime,true,_FlowTiling,_FlowOffest);
                    half4 texA = SampleMainTex(_MainTex,uvwA.xy);
                    texA*=uvwB.z;
                    half4 texB = SampleMainTex(_MainTex,uvwB.xy);
                    texB*=uvwB.z;
                    col.xyz = (texA.xyz+texB.xyz);
                #endif
                  
                #if _DISOLVE_ON
                   float3 disolve = tex2D(_DisolveTex,i.disUV).rgb;
                   clip(disolve.r-_DisloveAmount-0.001);           
				   float result = _DisloveAmount / disolve.r;
                   if(result>_DisloveRimColorLengt)
                   {
                       col.rgb+=_DisloveRimStartColor;
                   }
                   else
                   {
                       col.rgb+=_DisloveRimEndColor;
                   }
				#endif
				if(_RimLight>0)
                {
					half3 fresnel=0;
					half f=saturate(1-dot(normalize(i.viewDir),i.normal));	//外发光	
                    fresnel=pow(f,1/_RimPower)*_RimColor.rgb*_RimColor.a;
					col.rgb+=fresnel;
                }
                return col;
            }
            ENDHLSL
        }
    }
	CustomEditor "SProGUI"
}
